package com.google.codejam.roundA2013.bullseye;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class bullseye {

    public static void main(String[] args) {
        String inputFile = args[0];
        String outputFile = args[1];
        readAndSolve(inputFile, outputFile);
    }

    private static String getResult(double r, double t) {
        double k = 0;
        System.out.println("compute:" + "r=" + r+", t="+t);
        k = (Math.PI + Math.sqrt((Math.PI - 2 * r) * (Math.PI - 2 * r) + 8 * Math.PI * t)) / (4 * Math.PI);
        String integerString = String.valueOf(k);
        System.out.println("result:" +k);
        if (integerString.contains(".")) {
            return integerString.substring(0, integerString.indexOf("."));
        } else {
            return integerString;
        }
    }

    public static String readAndSolve(String inFileName, String outFileName) {
        File file = new File(inFileName);
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            FileWriter fileWriter = new FileWriter(outFileName, true);
            String readLine = null;
            int currentTestCaseIndex = 1;
            int line = 0;
            double r, t;

            while ((readLine = bufferedReader.readLine()) != null) {
                if (readLine.trim().length() <= 0) {
                    continue;
                }
                if (line == 0) {
                    line++;
                    continue;
                }
                String[] interval = readLine.split(" ");
                r = Double.valueOf(interval[0]);
                t = Double.valueOf(interval[1]);
                String result = getResult(r, t);
                fileWriter.append("Case #" + currentTestCaseIndex + ": " + result);
                fileWriter.append("\n");
                currentTestCaseIndex++;
            }
            fileWriter.flush();
            fileWriter.close();
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
